//
//  NoteView.swift
//  WYYao
//
//  Created by wangcl on 2024/9/10.
//

import SwiftUI

struct NoteView: View {
    @State var searchText = ""
    @State var showNewNoteView = false
    @State var noteItems:[NoteItem] = []
    var body: some View {
        NavigationView{
            ZStack {
                if noteItems.isEmpty {
                    NoDataView()
                }else{
                    VStack {
                        searchBarView()
                        NoteListView(noteItems: noteItems)
                    }
                }
                newNoteView()
            }.navigationBarTitle("念头笔记",displayMode: .inline)
        }.sheet(isPresented: $showNewNoteView, content: {
            NewNoteView(title: "",content: "",showNoteView: $showNewNoteView,noteItems: $noteItems)
        })
    }
    func searchBarView() -> some View{
        TextField("搜索内容",text:$searchText)
            .padding(7)
            .padding(.horizontal,25)
            .background(Color(.systemGray6))
            .cl_cornerRadius(5)
            .overlay (
                HStack {
                    Image(systemName: "magnifyingglass")
                        .cl_foregroundColor(.gray)
                        .frame(maxWidth: .infinity,alignment: .leading)
                        .padding(.leading,8)
                    if !searchText.isEmpty {
                        Button(action: {
                            self.searchText = ""
                        }, label: {
                            Image(systemName: "multiply.circle.fill")
                                .cl_foregroundColor(.gray)
                                .padding(.trailing,8)
                        })
                    }
                }
            ).padding(.horizontal,8)
        
    }
    
    //新建笔记按钮
    func newNoteView() -> some View{
        VStack{
            Spacer()
            HStack {
                Spacer()
                Button(action: {
                    showNewNoteView.toggle()
                }, label: {
                    Image(systemName: "plus.circle.fill").font(.system(size: 48)).cl_foregroundColor(.blue)
                })
            }
        }
        .padding(.bottom,32)
        .padding(.trailing,32)
    }
}

extension View {
    func cl_foregroundColor(_ color: Color) -> some View{
        if #available(iOS 15, *) {
            return foregroundStyle(color)
        } else {
            return foregroundColor(color)
        }
    }
    func cl_cornerRadius(_ value:CGFloat) -> some View{
        if #available(iOS 15, *) {
            return clipShape(RoundedRectangle(cornerRadius: value))
        } else {
            return cornerRadius(value)
        }
    }
}
#Preview {
    NoteView().navigationTitle("念头笔记")
}
